/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.util;

import org.apache.commons.lang3.StringUtils;

import com.yanlight.Constants;
import com.yanlight.support.BeanContext;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.exceptions.JedisException;

/**
 * 
 * @author haolw
 * @time 2017/07/28
 */
public abstract class JedisUtil {

	/**
	 * 
	 * @param key
	 * @return
	 */
	public static Boolean exists(String key) {
		Jedis jedis = getJedis();
		try {
			return jedis.exists(key);
		} catch (Exception e) {
			throw new JedisException(e);
		} finally {
			releaseJedis(jedis);
		}
	}

	/**
	 * set，json
	 * 
	 * @param key
	 * @param obj
	 */
	public static void set(String key, Object obj) {
		Jedis jedis = getJedis();
		try {
			String value = null;
			if (ClassUtil.isPrimitive(obj.getClass())) {
				value = String.valueOf(obj);
			} else {
				value = JsonUtil.toJson(obj);
			}

			jedis.set(key, value);
		} catch (Exception e) {
			throw new JedisException(e);
		} finally {
			releaseJedis(jedis);
		}
	}

	/**
	 * 设置过期值，单位秒
	 * 
	 * @param key
	 * @param obj
	 * @param expireSeconds
	 */
	public static void expire(String key, Object obj, int expireSeconds) {
		Jedis jedis = getJedis();
		try {
			String value = null;
			if (ClassUtil.isPrimitive(obj.getClass())) {
				value = String.valueOf(obj);
			} else {
				value = JsonUtil.toJson(obj);
			}
			jedis.set(key, value);
			jedis.expire(key, expireSeconds);
		} catch (Exception e) {
			throw new JedisException(e);
		} finally {
			releaseJedis(jedis);
		}
	}

	/**
	 * 设置过期值，单位秒
	 * 
	 * @param key
	 * @param obj
	 * @param expireSeconds
	 */
	public static void expire(String key, byte[] bytes, int expireSeconds) {
		Jedis jedis = getJedis();
		try {
			jedis.set(key.getBytes(Constants.DEFAULT_CHARSET), bytes);
			jedis.expire(key, expireSeconds);
			return;
		} catch (Exception e) {
			throw new JedisException(e);
		} finally {
			releaseJedis(jedis);
		}
	}

	/**
	 * 得到key，默认string
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		Jedis jedis = getJedis();
		try {
			return jedis.get(key);
		} catch (Exception e) {
			throw new JedisException(e);
		} finally {
			releaseJedis(jedis);
		}
	}

	/**
	 * 
	 * @param key
	 * @return
	 */
	public static byte[] getBytes(String key) {
		Jedis jedis = getJedis();
		try {
			return jedis.get(key.getBytes(Constants.DEFAULT_CHARSET));
		} catch (Exception e) {
			throw new JedisException(e);
		} finally {
			releaseJedis(jedis);
		}
	}

	/**
	 * 得到key
	 * 
	 * @param key
	 * @param clazz
	 * @return
	 */
	public static <T> T get(String key, Class<T> clazz) {
		Jedis jedis = getJedis();
		try {
			String content = jedis.get(key);
			if (StringUtils.isBlank(content)) {
				return null;
			} else {
				T t = JsonUtil.parse(content, clazz);
				return t;
			}
		} catch (Exception e) {
			throw new JedisException(e);
		} finally {
			releaseJedis(jedis);
		}
	}

	/**
	 * 
	 * @param channel
	 * @param message
	 */
	public static void publish(String channel, String message) {
		Jedis jedis = getJedis();
		try {
			jedis.publish(channel, message);
		} catch (Exception e) {
			throw new JedisException(e);
		} finally {
			releaseJedis(jedis);
		}
	}

	/**
	 * 
	 * @param jedisPubSub
	 * @param channels
	 */
	public static void subscribe(JedisPubSub jedisPubSub, String... channels) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				getJedis().subscribe(jedisPubSub, channels);
			}
		}).start();
	}

	/**
	 * 得到redis客户端
	 * 
	 * @return
	 */
	public static Jedis getJedis() {
		JedisPool jedisPool = BeanContext.getBean(JedisPool.class);
		Jedis jedis = jedisPool.getResource();
		return jedis;
	}

	/**
	 * 释放redis
	 * 
	 * @param jedis
	 */
	public static void releaseJedis(Jedis jedis) {
		jedis.close();
	}
}
